package lumis.portal.serialization;

import lumis.portal.PortalException;
import lumis.portal.PortalObjectNotFoundException;
import lumis.portal.authentication.SessionConfig;
import lumis.portal.manager.ManagerFactory;
import lumis.portal.service.ServiceConfig;
import lumis.portal.service.ServiceStringResource;
import lumis.portal.service.serialization.ServiceSerializationMessageConfig;
import lumis.portal.serviceinterface.ServiceInterfaceConfig;
import lumis.portal.serviceinterface.serialization.ServiceInterfaceSerializationMessageConfig;
import lumis.util.IResource;
import lumis.util.ITransaction;

/**
 * Class resposible for external dependencies localization
 *
 * @version $Revision: 9062 $ $Date: 2008-03-25 16:05:22 -0300 (Tue, 25 Mar 2008) $
 * @since 4.0.8
 */
public class ExternalDependencyLocalizer
{
	private SessionConfig sessionConfig;
	private ITransaction transaction;
	
	public ExternalDependencyLocalizer(SessionConfig sessionConfig, ITransaction transaction)
	{
		super();
		this.sessionConfig = sessionConfig;
		this.transaction = transaction;
	}

	public void localizeMessage(Dependency dependency) throws PortalException
	{
		localizeNode(dependency.getDependableNode());
		localizeNode(dependency.getDependentNode());
	}

	private void localizeNode(SerializationMessageConfigNode serializationMessageConfigNode) throws PortalException
	{
		if (serializationMessageConfigNode == null)
			return;
		SerializationMessageConfig serializationMessageConfig = serializationMessageConfigNode.getSerializationMessageConfig();
		if (serializationMessageConfig instanceof ServiceSerializationMessageConfig)
		{
			String serviceId = serializationMessageConfig.getId();
			IResource resource = null;
			ServiceConfig serviceConfig = null;
			try
			{
				serviceConfig = ManagerFactory.getServiceManager().get(sessionConfig, serviceId, transaction);
			}
			catch (PortalObjectNotFoundException e)
			{
			}
			if (serviceConfig != null)
				resource = serviceConfig.getResource();
			else
				resource = new ServiceStringResource();
			((ServiceSerializationMessageConfig) serializationMessageConfig).setName(ManagerFactory.getLocalizationManager().localize(
					((ServiceSerializationMessageConfig) serializationMessageConfig).getName(), resource, sessionConfig.getLocale()));
		}
		if (serializationMessageConfig instanceof ServiceInterfaceSerializationMessageConfig)
		{
			String serviceInterfaceId = serializationMessageConfig.getId();
			IResource resource = null;
			ServiceInterfaceConfig serviceInterfaceConfig = null;
			try
			{
				serviceInterfaceConfig = ManagerFactory.getServiceInterfaceManager().getServiceInterfaceConfig(serviceInterfaceId, transaction);
			}
			catch (PortalObjectNotFoundException e)
			{
			}
			if (serviceInterfaceConfig != null)
				resource = serviceInterfaceConfig.getResource();
			else
				resource = new ServiceStringResource();
			((ServiceInterfaceSerializationMessageConfig) serializationMessageConfig).setName(ManagerFactory.getLocalizationManager().localize(
					((ServiceInterfaceSerializationMessageConfig) serializationMessageConfig).getName(), resource, sessionConfig.getLocale()));
		}
		for (SerializationMessageConfigNode parentSerializationMessageConfigNode : serializationMessageConfigNode.getParentNodes())
			localizeNode(parentSerializationMessageConfigNode);
	}

	public void setTransaction(ITransaction transaction)
	{
		this.transaction = transaction;
	}
}
